<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Borrow extends MY_Controller {

    function __construct() {
        parent::__construct();
        /** 登陆验证 */
        if (!isset($_SESSION)) {
            session_start();
        }
        $admin = $this->session->userdata('admin_user');
        if (!$admin || !isset($admin ['user_id'])) {
            header('Location: /ychadmin/login');
            exit();
        }
        $this->tab = 'borrow';
    }

    private function subtab($qtype, $fstatus) {
        if ($qtype == "full") {
            $tab = 'full';
            return $tab;
        }

        if ($fstatus == 0) {
            $tab = 'first';
        } else if ($fstatus == 1) {
            $tab = 'now';
        } else if ($fstatus == 2) {
            $tab = 'liubiao';
        } else {
            $tab = 'all';
        }

        return $tab;
    }

    /**
     * 标列表
     */
    function borrowlist() {
        //接收参数
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;
        $title = $this->input->get('title');
        $username = $this->input->get('username');
        $fstatus = $this->input->get('fstatus');
        $ftype = $this->input->get('ftype');
        $qtype = $this->input->get('qtype');

        $data = array();
        if ($qtype == "full") {
            $data ['where'] ['qtype'] = "full";
        }
        if ($fstatus != - 2) {
            $data ['where'] ['fstatus'] = $fstatus;
        }
        if ($title != '') {
            $data ['where'] ['title'] = $title;
        }
        if ($username != '') {
            $data ['where'] ['username'] = $username;
        }
        if ($ftype != 0) {
            $data ['where'] ['ftype'] = $ftype;
        }
        $data ['page_size'] = YTZ_LIST_PAGE_SIZE;
        $data ['page_start'] = (int) $per_page;
        $data ['select'] = 1;
        $this->load->model('Borrow_model');
        $vdata = $this->Borrow_model->get_borrow_user_list($data);
		
        //分页
        $this->load->library('pagination');
        $config ['base_url'] = '/ychadmin/borrow/borrowlist/?title=' . $title . '&username=' . $username . '&fstatus=' . $fstatus . '&ftype=' . $ftype . '&qtype=' . $qtype;
        $config ['total_rows'] = $vdata ['borrow_count'];
        $config ['per_page'] = YTZ_LIST_PAGE_SIZE;
        $this->pagination->initialize($config);

        $vdata ['borrow_type'] = $this->Borrow_model->arrays('borrow_type'); //标种
        $vdata ['borrow_status'] = $this->Borrow_model->arrays('borrow_status'); //状态
        $vdata ['borrow_time_limit'] = $this->Borrow_model->arrays('borrow_time_limit'); //借款期限
        $vdata ['borrow_day_time_limit'] = $this->Borrow_model->arrays('borrow_day_time_limit'); //借款期限
        $vdata ['borrow_use'] = $this->Borrow_model->arrays('borrow_use'); //借款用途
        $vdata ['borrow_style'] = $this->Borrow_model->arrays('borrow_style'); //还款方式
        $vdata ['borrow_lowest_account'] = $this->Borrow_model->arrays('borrow_lowest_account'); //最低投标金额
        $vdata ['borrow_most_account'] = $this->Borrow_model->arrays('borrow_most_account'); //最多投标总额
        $vdata ['borrow_valid_time'] = $this->Borrow_model->arrays('borrow_valid_time'); //有效时间
        $vdata ['fstatus'] = $fstatus;
        $vdata ['ftype'] = $ftype;
        $vdata ['title'] = $title;
        $vdata ['username'] = $username;

        $vdata ['tab'] = $this->tab;
        $vdata ['sub'] = $this->subtab($qtype, $fstatus);
    
        $this->load->view('/ychadmin/borrow_list', $vdata);
    }

    /**
     * 撤标
     */
    function withdraw($borrow_id) {
        //接收参数
        $data = array('fstatus' => 2);
        $this->load->model('Borrow_model');
        $borrow_obj = $this->Borrow_model->get_borrow_obj($borrow_id, $data);

        $user = $this->session->userdata('ych_user');
        $data ['id'] = $borrow_id;
        $data ['user_id'] = $borrow_obj ['user_id'];
        $data ['ip'] = $this->js->get_ip();
        $this->load->model('log_model');
        $this->log_model->add_log('[撤标][借款标]');
        echo $this->Borrow_model->Cancel($data);
    }

    /**
     * 马甲标审核
     */
    function mjb_review($borrow_id) {
        $this->load->model('Borrow_model');
        $borrowobj = $this->Borrow_model->get_borrow_obj($borrow_id);
        $endTime = strtotime('+ ' . $borrowobj['time_limit'] . ' month');
        if ($borrow['is_day'] == 1) {
            $endTime = strtotime('+ ' . $borrowobj['time_limit_day'] . ' days');
        }
        $data = array('fstatus' => 3, 'account_yes' => $borrowobj ['account'], 'end_time' => $endTime);
        $mjb_tender = unserialize($borrowobj ['mjb_tender']);

        //模拟投资
        $zm26 = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
        $tmp = $borrowobj ['account'] - $borrowobj ['account_yes'];
        $ttmp = 0;
        for ($i = 0; $i < 30; $i ++) {
            //随机生成金额
            $rand = rand($borrowobj ['lowest_account'] / 100, $tmp / 100);
            $randmoney = $rand > 0 ? $rand : 1;
            $tmpr = intval($randmoney) * 100;

            if ($ttmp + $tmpr > $tmp) {
                $tmpr = $tmp - $ttmp;
            }
            $ttmp += $tmpr;

            //随机生成用户名
            $username_len = rand(6, 15);
            $tmpusername = "";
            for ($i = 0; $i < $username_len; $i ++) {
                $tmpusername .= $zm26 [rand(0, 25)];
            }
            if ($tmpr > 0) {
                $mjb_tender ['username'][] = $tmpusername;
                $mjb_tender['money'][] = $tmpr;
                $mjb_tender['time'][] = rand($borrowobj ['add_time'], time());
            }
            if ($ttmp == $tmp) {
                break;
            }
        }
        $data ['mjb_tender'] = serialize($mjb_tender);
        echo $this->Borrow_model->update($borrow_id, $data);
    }

    /**
     * 编辑借款标视图
     * @param unknown_type $borrow_id
     */
    function edit_view($borrow_id) {
        $data ['select'] = 1;
        $this->load->model('Borrow_model');
        $borrow = $this->Borrow_model->get_borrow_obj($borrow_id, $data);
        $vdata ['borrow_type'] = $this->Borrow_model->arrays('borrow_type'); //标种
        $vdata ['borrow_status'] = $this->Borrow_model->arrays('borrow_status'); //状态
        $vdata ['borrow_time_limit'] = $this->Borrow_model->arrays('borrow_time_limit'); //借款期限
        $vdata ['borrow_day_time_limit'] = $this->Borrow_model->arrays('borrow_day_time_limit'); //借款期限
        $vdata ['borrow_use'] = $this->Borrow_model->arrays('borrow_use'); //借款用途
        $vdata ['borrow_style'] = $this->Borrow_model->arrays('borrow_style'); //还款方式
        $vdata ['borrow_lowest_account'] = $this->Borrow_model->arrays('borrow_lowest_account'); //最低投标金额
        $vdata ['borrow_most_account'] = $this->Borrow_model->arrays('borrow_most_account'); //最多投标总额
        $vdata ['borrow_valid_time'] = $this->Borrow_model->arrays('borrow_valid_time'); //有效时间
        $vdata ['borrow'] = $borrow;
        $bTypeGrt = $this->Borrow_model->arrays('bTypeGrt');
        $guaranteeKey = $bTypeGrt [$borrow ['ftype']];

        $guaranteeList = $this->Borrow_model->get_guarantee_borrow_list();
        $arr = array();
        foreach ($guaranteeList as $list) {
            if ($list ['type'] == $guaranteeKey) {
                $arr [] = $list;
            }
        }
        $vdata ['guarantee_list'] = $arr;

        $vdata ['tab'] = $this->tab;
        $vdata ['sub'] = 'first';

        $this->load->view('/ychadmin/borrow_edit', $vdata);
    }

    //加水印
    public function has_mark($thumbnail) {
        $this->load->library('image_lib');
        $warter ['source_image'] = $thumbnail;
        $warter ['wm_type'] = 'overlay';
        $warter ['wm_overlay_path'] = WATERMARK_IMG;
        $warter ['wm_opacity'] = '60';
        $warter ['wm_vrt_alignment'] = 'middle';
        $warter ['wm_hor_alignment'] = 'center';
        $this->image_lib->initialize($warter);
        $this->image_lib->watermark();
    }

    function upload_borrow_thumbnail() {
        $path = BORROW_IMG;
        if (!file_exists($path)) {
            mkdir($path, 0777);
        }
        $config ['upload_path'] = $path;
        $config ['allowed_types'] = 'jpg|png';
        $config ['file_name'] = 'borrow_' . uniqid();
        $config ['overwrite'] = true;
        $config ['max_size'] = '512';
        $config ['max_width'] = '2048';
        $config ['max_height'] = '3000';
        $this->load->library('upload', $config);
        if ($this->upload->do_upload('thumbnail')) {
            $tmp = $this->upload->data();
            $thumbnail = $config ['file_name'] . $tmp ['file_ext'];

            //添加水印
            //$this->has_thumb($tmp);


            $this->load->library('image_lib');
            $warter ['source_image'] = $thumbnail;
            $warter ['wm_type'] = 'overlay';
            $warter ['wm_overlay_path'] = WATERMARK_IMG;
            $warter ['wm_opacity'] = '60';
            $warter ['wm_vrt_alignment'] = 'middle';
            $warter ['wm_hor_alignment'] = 'center';
            $this->image_lib->initialize($warter);
            $this->image_lib->watermark();
            return $thumbnail;
        } else {
            $ret ['url'] = 'error';
            $ret ['msg'] = $this->upload->display_errors();
            echo json_encode($ret);
            exit();
        }
    }

    /**
     * 编辑借款标
     */
    function edit() {
        $ret = array('msg' => '', 'url' => '');

        $data = array('project_number' => trim($this->input->post('project_number')), 'title' => trim($this->input->post('title')), 'ftype' => intval($this->input->post('ftype')), 'fstatus' => intval($this->input->post('fstatus')), 'fuse' => trim($this->input->post('fuse')), 'time_limit' => $this->input->post("time_limit") ? $this->input->post("time_limit") : 1, 'valid_time' => $this->input->post("valid_time"), 'style' => $this->input->post('style'), 'account' => $this->input->post('account'), 'apr' => $this->input->post('apr'), 'lowest_account' => $this->input->post('lowest_account'), 'most_account' => $this->input->post('most_account'), 'award' => $this->input->post('award'), 'funds' => $this->input->post('funds'), 'time_limit_day' => 0, 'content' => trim($this->input->post('content')), 'repay_from' => trim($this->input->post('repay_from')), 'risk_manage' => trim($this->input->post('risk_manage')), 'publish_remark' => trim($this->input->post('publish_remark')), 'verify_remark' => trim($this->input->post('verify_remark')), 'recommend_id' => intval($this->input->post('recommend_id')), 'guarantee_id' => intval($this->input->post('guarantee_id')), 'borrow_fee' => $this->input->post('borrow_fee'), 'is_mjb' => intval($this->input->post('is_mjb')));
        $id = intval($this->input->post('id'));
        if ($this->input->post('is_mjb') == 1) {
            $data ['is_dxb'] = 1;
            $data ['dxb_pwd'] = "1touzi";
            $data ['account_yes'] = $this->input->post('account_yes');

            //模拟投资
            $zm26 = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
            $temp_money = array();
            $temp_time = array();
            $temp_username = array();
            $tmp = $data ['account_yes'];
            $ttmp = 0;
            for ($i = 0; $i < 30; $i ++) {
                //随机生成金额
                if ($tmp <= $data ['lowest_account']) {
                    $randmoney = $tmp;
                } else {
                    $randmoney = rand($data ['lowest_account'] / 100, $tmp / 100);
                    $randmoney *= 100;
                }
                $tmp -= $randmoney;

                //投标时间
                $tenderTime = rand(time() - 10800, time()); //3小时
                //用户名
                $len = rand(6, 15);
                $tmpusername = "";
                for ($i = 0; $i < $len; $i ++) {
                    $tmpusername .= $zm26 [rand(0, 25)];
                }

                if ($randmoney > 0) {
                    $temp_money [] = $randmoney;
                    $temp_time [] = $tenderTime;
                    $temp_username [] = $tmpusername;
                }

                if ($tmp <= 0) {
                    break;
                }
            }
            sort($temp_time);
            $temptender = array('money' => $temp_money, "time" => $temp_time, "username" => $temp_username);
            $data ['mjb_tender'] = serialize($temptender);
        }

        //自动审核时间 ----start
        $auto_verify_time_post = $this->input->post("auto_verify_time");
        if (isset($auto_verify_time_post) && $auto_verify_time_post) {
            $min = date('i', strtotime($auto_verify_time_post));
            if ($min % 10 == 0) {
                $auto_verify_time = strtotime($auto_verify_time_post);
            } else {
                $m = round($min / 10) * 10;
                $new_time = date('Y-m-d H', strtotime($auto_verify_time_post));
                $auto_verify_time = strtotime($new_time . ':' . $m);
            }
            $data ['auto_verify_time'] = $auto_verify_time;
        }

        //自动审核时间 ----end


        if ($data ['account'] < 500 || $data ['account'] > 10000000) {
            $ret ['msg'] = '金额必须大于500且小于1000w';
        } elseif ($data ['apr'] < 0 || $data ['apr'] > 24) {
            $ret ['msg'] = '利率必须在0%-24%之间';
        } elseif ($data ['award'] == 2) {
            if ($data ['funds'] < 0)
                $ret ['msg'] = '请输入奖励比例';
        } elseif ($data ['title'] == '') {
            $ret ['msg'] = '标题不能为空';
        } elseif ($data ['content'] == '') {
            $ret ['msg'] = '内容不能为空';
        }
        if ($ret ['msg']) {
            echo json_encode($ret);
            exit();
        }

        $idata ['time_limit'] = $data ['time_limit'];
        $idata ['style'] = $data ['style'];
        $idata ['account'] = $data ['account'];
        $idata ['apr'] = $data ['apr'];
        ;
        $idata ['is_day'] = 0;
        $idata ['time_limit_day'] = 0;
        $idata ['type'] = "all";
        $interest_result = equal_interest($idata);
        $data ['repayment_account'] = $interest_result ['repayment_account'];
        $data ['monthly_repayment'] = $interest_result ['monthly_repayment'];
        if ($is_day == 1 && $time_limit == 1) {
            $data ['monthly_repayment'] = $interest_result ['repayment_account'];
        }

        $data ['update_time'] = time();

        if (isset($_FILES ['thumbnail']) && $_FILES ['thumbnail'] ['name'] != '') {
            echo $_FILES ['thumbnail'] ['name'].' ';
            $thumbnail = $this->upload_borrow_thumbnail();
            echo $thumbnail.' ';
            $data ['thumbnail'] = $thumbnail;
        }

        $this->load->model('Borrow_model');
        $falg = $this->Borrow_model->update($id, $data);
        $ret ['msg'] = $falg;
        $this->load->model('log_model');
        $this->log_model->add_log('[修改][借款标][ID==' . $id . ']');
        echo json_encode($ret);
    }

    function repayment() {
        //接收参数
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;
        $title = $this->input->get('title');
        $username = $this->input->get('username');
        $strtime = $this->input->get('strtime');
        $endtime = $this->input->get('endtime');
        $fstatus = $this->input->get('fstatus');

        $data = array();
        if ($fstatus != - 1) {
            $data ['where'] ['fstatus'] = $fstatus;
        }
        if ($title != '') {
            $data ['where'] ['title'] = $title;
        }
        if ($username != '') {
            $data ['where'] ['username'] = $username;
        }
        if ($strtime != '') {
            $data ['where'] ['strtime'] = $strtime;
        }
        if ($endtime != '') {
            $data ['where'] ['endtime'] = $endtime;
        }

        $data ['page_size'] = YTZ_LIST_PAGE_SIZE;
        $data ['page_start'] = (int) $per_page;
        $data ['select'] = 1;
        $this->load->model('User_model');
        $this->load->model('Borrow_model');
        $vdata = $this->Borrow_model->get_repayment_list($data);
        $repayment_list = $vdata ['repayment_list'];

        $repayment_list_new = array();
        foreach ($repayment_list as $repayment) {
            $user_obj = $this->User_model->get_by_uid($repayment ['user_id']);
            $repayment ['username'] = $user_obj ['username'];
            //$repayment['repayment_account_majia'] = $this->Borrow_model->get_borrow_collection_majia(array('borrow_id'=>$repayment['borrow_id'],'forder'=>$repayment['forder']));
            $repayment ['repayment_account_majia'] = $this->Borrow_model->get_borrow_collection_majia(array('fstatus' => $fstatus, 'borrow_id' => $repayment ['borrow_id'], 'forder' => $repayment ['forder'], 'strtime' => $strtime, 'endtime' => $endtime));
            $repayment_list_new [] = $repayment;
        }
        $vdata ['repayment_list'] = $repayment_list_new;

        //分页
        $this->load->library('pagination');
        $config ['base_url'] = '/ychadmin/borrow/repayment/?title=' . $title . '&username=' . $username . '&fstatus=' . $fstatus;
        $config ['total_rows'] = $vdata ['repayment_count'];
        $config ['per_page'] = YTZ_LIST_PAGE_SIZE;
        $this->pagination->initialize($config);

        $vdata ['fstatus'] = $fstatus;
        $vdata ['title'] = $title;
        $vdata ['username'] = $username;
        $vdata ['strtime'] = $strtime;
        $vdata ['endtime'] = $endtime;

        $vdata ['tab'] = 'account';
        $vdata ['sub'] = 'repayment';

        $this->load->view('/ychadmin/repayment_list', $vdata);
    }

    function lz_repayment() {
        //接收参数
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;
        $title = $this->input->get('title');
        $strtime = $this->input->get('strtime');
        $endtime = $this->input->get('endtime');
        $fstatus = $this->input->get('fstatus');

        $data = array();
        if ($fstatus != - 1) {
            $data ['where'] ['fstatus'] = $fstatus;
        }
        if ($title != '') {
            $data ['where'] ['title'] = $title;
        }
        if ($strtime != '') {
            $data ['where'] ['strtime'] = $strtime;
        }
        if ($endtime != '') {
            $data ['where'] ['endtime'] = $endtime;
        }

        $data ['page_size'] = YTZ_LIST_PAGE_SIZE;
        $data ['page_start'] = (int) $per_page;
        $data ['select'] = 1;
        $data ['order'] = 1;
        $this->load->model('User_model');
        $this->load->model('Borrow_model');
        $vdata = $this->Borrow_model->get_lz_repayment_huizong_list($data);
        $repayment_list = $vdata ['repayment_list'];

        $repayment_list_new = array();
        foreach ($repayment_list as $repayment) {
            $user_obj = $this->User_model->get_by_uid($repayment ['user_id']);
            $repayment ['username'] = $user_obj ['username'];
            $repayment ['repayment_account_majia'] = $this->Borrow_model->get_borrow_collection_majia(array('fstatus' => $fstatus, 'borrow_id' => $repayment ['id'], 'forder' => $repayment ['forder'], 'strtime' => $strtime, 'endtime' => $endtime));
            $repayment_list_new [] = $repayment;
        }
        $vdata ['repayment_list'] = $repayment_list_new;

        //总预还金额
        //		$data['limit'] = 'all';
        //		$real_vdata = $this->Borrow_model->get_lz_repayment_huizong_list($data);
        //		$repayment_real_list = $real_vdata['repayment_list'];
        //		$repayment_list_real = 0;
        //		$total_repayment = 0;
        //		foreach($repayment_real_list as $repayment){
        //			$majia_money = $this->Borrow_model->get_borrow_collection_majia(array('fstatus'=>$fstatus,'borrow_id'=>$repayment['id'],'forder'=>$repayment['forder'],'strtime'=>$strtime,'endtime'=>$endtime));
        //			$real_money = $repayment['repay_accounts'] - $majia_money;
        //			$repayment_list_real = $repayment_list_real+$real_money;
        //			$total_repayment = $total_repayment+$repayment['repay_accounts'];
        //		}
        //		$vdata['realmoney'] = $repayment_list_real;
        //		$vdata['totalmoney'] = $vdata['repayment_totalmoney'];//$total_repayment;//
        //分页
        $this->load->library('pagination');
        $config ['base_url'] = '/ychadmin/borrow/lz_repayment/?title=' . $title . '&fstatus=' . $fstatus . '&strtime=' . $strtime . '&endtime=' . $endtime;
        $config ['total_rows'] = $vdata ['repayment_count'];
        $config ['per_page'] = YTZ_LIST_PAGE_SIZE;
        $this->pagination->initialize($config);

        $vdata ['fstatus'] = $fstatus;
        $vdata ['title'] = $title;
        $vdata ['strtime'] = $strtime;
        $vdata ['endtime'] = $endtime;

        $vdata ['tab'] = 'account';
        $vdata ['sub'] = 'lz_repayment';

        $this->load->view('/ychadmin/lz_repayment_list', $vdata);
    }

    function lzrepaymentexcel() {
        //接收参数
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;
        $title = $this->input->get('title');
        $strtime = $this->input->get('strtime');
        $endtime = $this->input->get('endtime');
        $fstatus = $this->input->get('fstatus');

        $data = array();
        if ($fstatus != - 1) {
            $data ['where'] ['fstatus'] = $fstatus;
        }
        if ($title != '') {
            $data ['where'] ['title'] = $title;
        }
        if ($strtime != '') {
            $data ['where'] ['strtime'] = $strtime;
        }
        if ($endtime != '') {
            $data ['where'] ['endtime'] = $endtime;
        }

        $data ['select'] = 1;
        $data ['order'] = 1;
        $data ['limit'] = 'all';
        $this->load->model('User_model');
        $this->load->model('Borrow_model');
        $vdata = $this->Borrow_model->get_lz_repayment_huizong_list($data);
        $repayment_list = $vdata ['repayment_list'];
        $repayment_list_new = array();
        foreach ($repayment_list as $repayment) {
            $user_obj = $this->User_model->get_by_uid($repayment ['user_id']);
            $repayment ['username'] = $user_obj ['username'];
            //$repayment['repayment_account_majia'] = $this->Borrow_model->get_borrow_collection_majia(array('borrow_id'=>$repayment['id'],'forder'=>$repayment['forder']));
            $repayment ['repayment_account_majia'] = $this->Borrow_model->get_borrow_collection_majia(array('fstatus' => $fstatus, 'borrow_id' => $repayment ['id'], 'forder' => $repayment ['forder'], 'strtime' => $strtime, 'endtime' => $endtime));
            $repayment_list_new [] = $repayment;
        }
        $vdata ['repayment_list'] = $repayment_list_new;

        $this->load->library('PHPExcel');
        $this->load->library('PHPExcel/IOFactory');
        $objPHPExcel = new PHPExcel ();
        $objPHPExcel->getProperties()->setTitle("还款列表")->setDescription(COMPANY_NAME);
        $objPHPExcel->setActiveSheetIndex(0);
        $titles = array("编号", "借款标题", "还款金额", "真实金额", "马甲金额", "还款时间");

        for ($i = 0; $i < count($titles); $i ++) {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles [$i]);
        }
        $repayment_list = $vdata ['repayment_list'];
        for ($i = 0; $i < count($repayment_list); $i ++) {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i + 2, $repayment_list [$i] ['id']);
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i + 2, $repayment_list [$i] ['title']);
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i + 2, $repayment_list [$i] ['repay_accounts']);
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i + 2, $repayment_list [$i] ['repay_accounts'] - $repayment_list [$i] ['repayment_account_majia']);
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i + 2, $repayment_list [$i] ['repayment_account_majia']);
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $i + 2, date('Y-m-d', $repayment_list [$i] ['repay_time']));
        }

        $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
        //发送标题强制用户下载文件
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="流转标还款列表.xls"');
        header('Cache-Control: max-age=0');
        $objWriter->save('php://output');
    }

    function repaymentexcel() {
        $title = $this->input->get('title');
        $username = $this->input->get('username');
        $strtime = $this->input->get('strtime');
        $endtime = $this->input->get('endtime');
        $fstatus = $this->input->get('fstatus');

        $data = array();
        if ($fstatus != - 1) {
            $data ['where'] ['fstatus'] = $fstatus;
        }
        if ($title != '') {
            $data ['where'] ['title'] = $title;
        }
        if ($username != '') {
            $data ['where'] ['username'] = $username;
        }
        if ($strtime != '') {
            $data ['where'] ['strtime'] = $strtime;
        }
        if ($endtime != '') {
            $data ['where'] ['endtime'] = $endtime;
        }

        $data ['select'] = 1;
        $this->load->model('Borrow_model');
        $this->load->model('User_model');
        $vdata = $this->Borrow_model->get_repayment_list($data);
        $repayment_list = $vdata ['repayment_list'];
        $repayment_list_new = array();
        foreach ($repayment_list as $repayment) {
            $user_obj = $this->User_model->get_by_uid($repayment ['user_id']);
            $repayment ['username'] = $user_obj ['username'];
            $repayment ['repayment_account_majia'] = $this->Borrow_model->get_borrow_collection_majia(array('borrow_id' => $repayment ['borrow_id'], 'forder' => $repayment ['forder']));
            $repayment_list_new [] = $repayment;
        }
        $vdata ['repayment_list'] = $repayment_list_new;

        $this->load->library('PHPExcel');
        $this->load->library('PHPExcel/IOFactory');
        $objPHPExcel = new PHPExcel ();
        $objPHPExcel->getProperties()->setTitle("还款列表")->setDescription(COMPANY_NAME);
        $objPHPExcel->setActiveSheetIndex(0);
        $titles = array("编号", "借款户", "借款标题", "期数", "到期时间", "还款金额", '实际金额', "马甲金额","本金", "利息", "还款时间");

        for ($i = 0; $i < count($titles); $i ++) {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles [$i]);
        }
        $repayment_list = $vdata ['repayment_list'];
        for ($i = 0; $i < count($repayment_list); $i ++) {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i + 2, $repayment_list [$i] ['id']);
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i + 2, $repayment_list [$i] ['username']);
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i + 2, $repayment_list [$i] ['title']);
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i + 2, ($repayment_list [$i] ['forder'] + 1) . '/' . $repayment_list [$i] ['time_limit']);
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i + 2, date('Y-m-d H:i:s', $repayment_list [$i] ['repayment_time']));
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $i + 2, number_format($repayment_list [$i] ['repayment_account'],2));
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $i + 2, $repayment_list [$i] ['repayment_account'] - $repayment_list [$i] ['repayment_account_majia']);
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, $i + 2, number_format($repayment_list [$i] ['repayment_account_majia'],2));
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(8, $i + 2, number_format($repayment_list [$i] ['capital'],2));
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(9, $i + 2, number_format($repayment_list [$i] ['interest'],2));
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(10, $i + 2, ($repayment_list [$i] ['repayment_yestime'] > 0)?date('Y-m-d H:i:s', $repayment_list [$i] ['repayment_yestime']):'');
        }

        $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
        //发送标题强制用户下载文件
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="还款列表.xls"');
        header('Cache-Control: max-age=0');
        $objWriter->save('php://output');
    }

    /**
     * 审标
     */
    function verify_view($borrow_id) {
        $data ['select'] = 1;
        $this->load->model('Borrow_model');
        $this->load->model('User_model');

        $borrow = $this->Borrow_model->get_borrow_obj($borrow_id, $data);

        $userinfo = $this->User_model->get_by_uid($borrow ['user_id']);
        $vdata ['userinfo'] = $userinfo;
        $borrow ['username'] = $userinfo ['username'];

        $this->load->model('Attestation_model', 'attestation');
        $this->load->model('Attestation_type_model', 'attestation_type');
        $vdata ['type_list'] = $this->attestation_type->get_id_names();
        $vdata ['status_list'] = $this->attestation->fstatus();
        $list = $this->attestation->get_list(array('user_id' => $borrow ['user_id'], 'is_del' => 0));
        $vdata ['list'] = $list ? $list : array();

        $vdata ['borrow_type'] = $this->Borrow_model->arrays('borrow_type'); //标种
        $vdata ['borrow_status'] = $this->Borrow_model->arrays('borrow_status'); //状态
        $vdata ['borrow_time_limit'] = $this->Borrow_model->arrays('borrow_time_limit'); //借款期限
        $vdata ['borrow_day_time_limit'] = $this->Borrow_model->arrays('borrow_day_time_limit'); //借款期限
        $vdata ['borrow_use'] = $this->Borrow_model->arrays('borrow_use'); //借款用途
        $vdata ['borrow_style'] = $this->Borrow_model->arrays('borrow_style'); //还款方式
        $vdata ['borrow_valid_time'] = $this->Borrow_model->arrays('borrow_valid_time'); //有效时间
        $vdata ['borrow'] = $borrow;

        $vdata ['tab'] = $this->tab;
        $vdata ['sub'] = 'first';
        $this->load->view('/ychadmin/borrow_verify', $vdata);
    }

    /**
     * 审标
     */
    function verify() {
        $id = $this->input->post('id');
        $fstatus = $this->input->post('fstatus');
        $verify_remark = $this->input->post('verify_remark');
        $ismjb = intval($this->input->post("is_mjb"));

        $this->load->model('Borrow_model');
        $borrow = $this->Borrow_model->get_borrow_obj($id);
        if ($borrow ['fstatus'] != 0) {
            echo "该标不处于待审核状态";
            return;
        }
        $type = array(1,2,3,7);
        if(!in_array($borrow['ftype'],$type) && $ismjb == 1){
            echo "只能设信用，担保，净值，抵押为新手标";
            return;
        }

        $data ["fstatus"] = $fstatus;
        $data ["verify_remark"] = $verify_remark;
        $data ["verify_time"] = time();
        $data ['is_check'] = 1;
        //是否设置为新手标1：是；0：否；
        $data ['is_mjb'] = $ismjb;
        $admin_user = $this->session->userdata('admin_user');
        $data ["verify_user"] = $admin_user ['user_id'];
        $flag = $this->Borrow_model->verify($id, $data);
        $this->log->write_log('info', $flag);
        //todo
//        if ($flag == 1 && $fstatus == 1) { //自动投标
//            $this->Notice(9, $borrow ['user_id']);
//
//            if ($borrow ['is_dxb'] != 1 && $borrow ['is_day'] != 1) { //自动投标
//                $this->Borrow_model->auto_tender_borrow($id);
//            }
//        }
        $this->load->model('log_model');
        $this->log_model->add_log('[审核][发标]');
        echo $flag;
    }

    /**
     * 复审
     */
    function review_view($borrow_id) {
        $data ['select'] = 1;
        $this->load->model('Borrow_model');
        $borrow = $this->Borrow_model->get_borrow_obj($borrow_id, $data);
        $vdata ['borrow_type'] = $this->Borrow_model->arrays('borrow_type'); //标种
        $vdata ['borrow_status'] = $this->Borrow_model->arrays('borrow_status'); //状态
        $vdata ['borrow_time_limit'] = $this->Borrow_model->arrays('borrow_time_limit'); //借款期限
        $vdata ['borrow_day_time_limit'] = $this->Borrow_model->arrays('borrow_day_time_limit'); //借款期限
        $vdata ['borrow_use'] = $this->Borrow_model->arrays('borrow_use'); //借款用途
        $vdata ['borrow_style'] = $this->Borrow_model->arrays('borrow_style'); //还款方式
        $vdata ['borrow_lowest_account'] = $this->Borrow_model->arrays('borrow_lowest_account'); //最低投标金额
        $vdata ['borrow_most_account'] = $this->Borrow_model->arrays('borrow_most_account'); //最多投标总额
        $vdata ['borrow_valid_time'] = $this->Borrow_model->arrays('borrow_valid_time'); //有效时间
        $vdata ['borrow'] = $borrow;
        $this->load->view('/ychadmin/borrow_review', $vdata);
    }

    /**
     * 复审
     */
    function review() {
        $id = $this->input->post('id');
        $fstatus = $this->input->post('fstatus');
        $repayment_remark = $this->input->post('repayment_remark');

        $data['id'] = $id;
        $data['ip'] = $this->js->get_ip();
        $data["fstatus"] = $fstatus;
        $data["repayment_remark"] = $repayment_remark;
        $data["repayment_user"] = 1;
        $this->load->model('Borrow_model');
        $flag = 0;
        if ($fstatus == 3) {//复审通过
            $flag = $this->Borrow_model->borrow_review_true($data);
        } elseif ($fstatus == 4) {//复审不通过
            $flag = $this->Borrow_model->borrow_review_false($data);
        }
        $this->load->model('log_model');
        $this->log_model->add_log('[复审][发标]');
        echo $flag;
    }

    /**
     * 撤标
     */
    function withdrawing($borrow_id) {
        $id = $borrow_id;

        $data ['id'] = $id;
        $data ['ip'] = $this->js->get_ip();
        $data ["fstatus"] = 4;
        $data ["repayment_remark"] = "";
        $data ["repayment_user"] = 1;
        $this->load->model('Borrow_model');
        $flag = 0;

        $data ['where'] ['borrow_id'] = $id;
        $data ['list'] = 1;
        $data ['page_size'] = 1000;
        $data ['page_start'] = 0;
        $tenderlist = $this->Borrow_model->get_borrow_tender_lists($data);
        $LoanNoList = "";
        foreach ($tenderlist as $tender) {
            if($tender ['mmm_loanno'] != '') {
                $LoanNoList .= $tender ['mmm_loanno'] . ",";
            }
        }
        if ($LoanNoList != "") {
            $LoanNoList = substr($LoanNoList, 0, strlen($LoanNoList) - 1);
            $reviewstr = $data ['id'] . '=' . $data ['ip'] . '=' . $data ['fstatus'] . '=' . $data ['repayment_remark'] . '=' . $data ['repayment_user'];
            self::toloantransferaudit($LoanNoList, $id, 4, 1, 1, $reviewstr);
        } else {
            $bj = $this->Borrow_model->get_borrow_obj($id);
            $data ['user_id'] = $bj ['user_id'];
            echo $this->Borrow_model->Cancel($data);
//            if ($this->Borrow_model->Cancel($data)) {
//                echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
//                echo "<script>alert('操作成功');window.close();</script>";
//            } else {
//                echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
//                echo "<script>alert('操作失败');window.close();</script>";
//            }
        }
    }

    function repay($repayment_id, $user_id) {
        $data ['repayment_id'] = $repayment_id;
        $data ['user_id'] = $user_id;
        $data ['ip'] = $this->js->get_ip();
        $this->load->model('Borrow_model');
        echo $this->Borrow_model->repay($data);
    }

    /**
     * 回款排行
     */
    function repayment_top() {
        $strtime = $this->input->get('strtime');
        $endtime = $this->input->get('endtime');
        $username = $this->input->get('username');
        $data ['where'] ['strtime'] = $strtime;
        $data ['where'] ['endtime'] = $endtime;
        $data ['where'] ['username'] = $username;
        $this->load->model('Borrow_model');
        $vdata ['repayment_list'] = $this->Borrow_model->repayment_top($data);
        $vdata ['strtime'] = $strtime;
        $vdata ['endtime'] = $endtime;
        $vdata ['username'] = $username;
        $this->load->view('/ychadmin/repayment_top', $vdata);
    }

    /**
     * 月统计
     */
    function index() {
        $this->load->model('Borrow_model');
        //$data = $this->Borrow_model->admin_index($data);


        $data ['total_data'] = $this->Borrow_model->admin_total_index();

        $data ['tab'] = $this->tab;
        $data ['sub'] = 'month';

        $this->load->view('/ychadmin/borrow_index', $data);
    }

    private function tender_data($limit = false) {
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;
        $username = $this->input->get('username');
        $is_self = isset($_GET ['is_self']) ? $this->input->get('is_self') : - 1;
        $fstatus = isset($_GET ['fstatus']) ? $this->input->get('fstatus') : - 1;
        $strtime = $this->input->get('strtime');
        $endtime = $this->input->get('endtime');

        $title = $this->input->get('title');
        $time_limit = $this->input->get('time_limit');

        if ($title != '') {
            $data ['where'] ['title'] = $title;
        }

        if ($time_limit != '' && $time_limit != - 1) {
            $data ['where'] ['time_limit'] = $time_limit;
        }

        if ($username != '') {
            $data ['where'] ['username'] = $username;
        }

        if ($is_self != '' && $is_self != - 1) {
            $data ['where'] ['is_self'] = $is_self;
        }

        if ($fstatus != '' && $fstatus != - 1) {
            $data ['where'] ['fstatus'] = $fstatus;
        }

        $data ['where'] ['strtime'] = $strtime;
        $data ['where'] ['endtime'] = $endtime;
        $data ['page_size'] = YTZ_LIST_PAGE_SIZE;
        $data ['page_start'] = (int) $per_page;
        $data ['limit'] = $limit;

        $this->load->model('Borrow_model');
        $vdata = $this->Borrow_model->borrow_tender_user_list($data);
        $tender_list = $vdata ['tender_list'];
        $tender_list1 = array();
        foreach ($tender_list as $tender) {
            $first_tender_obj = $this->Borrow_model->user_first_tender($tender ['user_id']);
            if ($first_tender_obj ['id'] == $tender ['id']) {
                $tender ['flag'] = 1;
            }
            $tender_list1 [] = $tender;
        }
        $vdata ['tender_list'] = $tender_list1;
        $vdata ['user_self'] = $this->Borrow_model->arrays("user_self");
        $vdata ['borrow_tender_status'] = $this->Borrow_model->arrays("borrow_tender_status");
        $vdata ['borrow_time_limit'] = $this->Borrow_model->arrays("borrow_time_limit");

        $vdata ['username'] = $username;
        $vdata ['is_self'] = $is_self;
        $vdata ['fstatus'] = $fstatus;
        $vdata ['strtime'] = $strtime;
        $vdata ['endtime'] = $endtime;
        $vdata ['title'] = $title;
        $vdata ['time_limit'] = $time_limit;

        $vdata ['user_count'] = $vdata ['tender_user_count'];
        $vdata ['tender_money'] = $vdata ['tender_money_count'];

        return $vdata;
    }

    function tenderlist() {

        $vdata = $this->tender_data();

        //分页
        $this->load->library('pagination');
        $config ['base_url'] = '/ychadmin/borrow/tenderlist/?strtime=' . $vdata ['strtime'] . '&endtime=' . $vdata ['endtime'] . '&username=' . $vdata ['username'] . '&fstatus=' . $vdata ['fstatus'];
        $config ['total_rows'] = $vdata ['tender_count'];
        $config ['per_page'] = YTZ_LIST_PAGE_SIZE;
        $this->pagination->initialize($config);

        $vdata ['tab'] = $this->tab;
        $vdata ['sub'] = 'tender';

        $this->load->view('/ychadmin/tender_list', $vdata);
    }

    /**
     * 流转标撤销投标
     */
    function cancel_tender() {
        $tid = intval($this->input->post('tid'));
        $this->load->model('Borrow_model');
        $msg = $this->Borrow_model->tender_cancel($tid);
        echo $msg;
        exit();
    }

    function tenderlistexcel() {

        $vdata = $this->tender_data('all');

        $this->load->library('PHPExcel');
        $this->load->library('PHPExcel/IOFactory');

        $objPHPExcel = new PHPExcel ();
        $objPHPExcel->getProperties()->setTitle("投标记录")->setDescription(COMPANY_NAME);

        $objPHPExcel->setActiveSheetIndex(0);
        $titles = array("用户编号", "投资客", "借款标", "投标金额", "期限", "投标时间");

        for ($i = 0; $i < count($titles); $i ++) {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles [$i]);
        }

        $tender_list = $vdata ['tender_list'];

        for ($i = 0; $i < count($tender_list); $i ++) {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i + 2, $tender_list [$i] ['id']);
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i + 2, $tender_list [$i] ['username']);
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i + 2, $tender_list [$i] ['title']);
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i + 2, number_format($tender_list [$i] ['account'], 2));
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i + 2, $tender_list [$i] ['time_limit']);
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $i + 2, date('Y-m-d H:i:s', $tender_list [$i] ['add_time']));
        }

        $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="投标记录.xls"');
        header('Cache-Control: max-age=0');
        $objWriter->save('php://output');
        exit();
    }

    function borrow_account() {
        $this->load->model('Borrow_model');
        $post_data = $this->input->post();
        $id = intval($post_data ['id']);
        $lowest = intval($post_data ['lowest_account']);
        $most = intval($post_data ['most_account']);
        $borrow_info = $this->Borrow_model->get_borrow_obj($id);
        if ($lowest < 100) {
            echo "最低投标金额不能小于100元";
            exit();
        } else if ($most != 0 && $lowest > $most) {
            echo "最低投标金额不能大于最高投标金额";
            exit();
        } else if ($most != 0 && $most > $borrow_info ['account']) {
            echo "最高投标金额不能大于借贷总金额";
            exit();
        }

        $ret = $this->Borrow_model->update($id, array('lowest_account' => $lowest, 'most_account' => $most));

        echo $ret ? '操作成功' : '操作失败';
    }

    /**
     *
     * 投标详细记录
     */
    function borrow_view($borrow_id) {
        $this->load->model('Borrow_model');
        $this->load->model('User_model');

        $data ['select'] = 1;
        $borrow = $this->Borrow_model->get_borrow_obj($borrow_id, $data);
        $vdata ['borrow_type'] = $this->Borrow_model->arrays('borrow_type'); //标种
        $vdata ['borrow_status'] = $this->Borrow_model->arrays('borrow_status'); //状态
        $vdata ['borrow_time_limit'] = $this->Borrow_model->arrays('borrow_time_limit'); //借款期限
        $vdata ['borrow_day_time_limit'] = $this->Borrow_model->arrays('borrow_day_time_limit'); //借款期限
        $vdata ['borrow_use'] = $this->Borrow_model->arrays('borrow_use'); //借款用途
        $vdata ['borrow_style'] = $this->Borrow_model->arrays('borrow_style'); //还款方式
        $vdata ['borrow_valid_time'] = $this->Borrow_model->arrays('borrow_valid_time'); //有效时间
        $vdata ['borrow'] = $borrow;

        //投标记录信息
        $borrow_tender_list = $this->Borrow_model->get_borrow_tender(array('select' => 1, 'where' => array('borrow_id' => $borrow_id)));
        $borrow_tender_list1 = array();
        foreach ($borrow_tender_list as $tender) {
            $user_obj = $this->User_model->get_by_uid($tender ['user_id']);
            $tender ['username'] = $user_obj ['username'];
            $borrow_tender_list1 [] = $tender;
        }
        $vdata ['borrow_tender_list'] = $borrow_tender_list1;

        //资料证明信息
        $this->load->model('User_model');
        $this->load->model('Attestation_model', 'attestation');
        $this->load->model('Attestation_type_model', 'attestation_type');

        $userinfo = $this->User_model->get_by_uid($borrow ['user_id']);
        $vdata ['userinfo'] = $userinfo;
        $borrow ['username'] = $userinfo ['username'];

        $vdata ['type_list'] = $this->attestation_type->get_id_names();
        $vdata ['status_list'] = $this->attestation->fstatus();
        $list = $this->attestation->get_list(array('user_id' => $borrow ['user_id'], 'is_del' => 0));
        $vdata ['list'] = $list ? $list : array();

        $borrow_id = $_GET ['borrow_id'] ? $_GET ['borrow_id'] : $borrow_id;
        $borrow_comment_count = $this->Borrow_model->get_borrow_comment_list(array('borrow_id' => $borrow_id, 'limit' => 'all'));

        //标评论
        $num = 4;
        //分页
        $page_obj = new stdClass ();
        $post_data = $this->input->get();
        $page_obj->num_results = $borrow_comment_count ['count'];
        $page_obj->num_pages = ceil($page_obj->num_results / $num);
        $page_obj->pg = isset($post_data ['pg']) ? intval($post_data ['pg']) : 1;
        $page_obj->pg = min($page_obj->pg, $page_obj->num_pages);
        $page_obj->pg = max($page_obj->pg, 1);
        $start_from = ($page_obj->pg - 1) * $num;
        $page_obj->paging_url = "javascript:load_comment('{$borrow_id}', {p})";

        $borrow_comment_list = $this->Borrow_model->get_borrow_comment_list(array('borrow_id' => $borrow_id), $start_from, $num);
        $pre_obj = new stdClass ();

        //加载列表
        $response_html = '';

        foreach ($borrow_comment_list ['list'] as $tmp) {
            $pre_obj->p = $tmp;
            $bdata ['D'] = $pre_obj;
            $response_html .= $this->load->view('/ychadmin/common/borrow_comment.php', $bdata, true);
        }

        $page_data ['D'] = $page_obj;

        $response_html .= $this->load->view('/ychadmin/common/paging.php', $page_data, true);

        $from = trim($this->input->get('from'));
        if (isset($from) && $from == 'ajax') {
            echo $response_html;
            exit();
        }

        $vdata ['response_html'] = $response_html;

        $vdata ['borrow_comment_list'] = $borrow_comment_list ['list'];

        $vdata ['guarantee_name_list'] = $this->Borrow_model->get_guarantee_name_list();

        $vdata ['tab'] = $this->tab;
        $vdata ['sub'] = 'all';

        $this->load->view('/ychadmin/borrow_view', $vdata);
    }

    function borrow_single($id) {
        $this->load->model('Borrow_model');
        $data ['id'] = $id;
        $data ['ip'] = $this->js->get_ip();
        echo $this->Borrow_model->borrow_review_single_false($data);
    }

    function aprshow() {
        $data = array();
        $act = $this->input->post('act');
        $key = 'index_apr_remark';
        if ($act == 'set') {
            $arr = array('apr0' => $this->input->post('apr0'), 'apr1' => $this->input->post('apr1'), 'apr3' => $this->input->post('apr3'), 'apr6' => $this->input->post('apr6'), 'apr9' => $this->input->post('apr9'), 'apr12' => $this->input->post('apr12'), 'apr18' => $this->input->post('apr18'), 'apr24' => $this->input->post('apr24'), 'apr36' => $this->input->post('apr36'), 'award0' => $this->input->post('award0'), 'award1' => $this->input->post('award1'), 'award3' => $this->input->post('award3'), 'award6' => $this->input->post('award6'), 'award9' => $this->input->post('award9'), 'award12' => $this->input->post('award12'), 'award18' => $this->input->post('award18'), 'award24' => $this->input->post('award24'), 'award36' => $this->input->post('award36'), 'shouyi0' => $this->input->post('shouyi0'), 'shouyi1' => $this->input->post('shouyi1'), 'shouyi3' => $this->input->post('shouyi3'), 'shouyi6' => $this->input->post('shouyi6'), 'shouyi9' => $this->input->post('shouyi9'), 'shouyi12' => $this->input->post('shouyi12'), 'shouyi18' => $this->input->post('shouyi18'), 'shouyi24' => $this->input->post('shouyi24'), 'shouyi36' => $this->input->post('shouyi36'));
            $this->mycache->set_array($key, $arr);
            echo "<script>alert('保存成功');</script>";
            exit();
        }
        $arr = $this->mycache->get_array($key);
        $data ['arr'] = $arr;
        $data ['title'] = "利率明细设置";

        $data ['tab'] = $this->tab;
        $data ['sub'] = 'interest';

        $this->load->view('/ychadmin/borrow_aprshow', $data);
    }

    function tomorrow() {
        $data = array();
        $act = $this->input->post('act');
        $key = 'index_borrow_tomorrow';
        if ($act == 'set') {
            $arr = array('bt_date' => $this->input->post('bt_date'), 'r1t1' => $this->input->post('r1t1'), 'r1t2' => $this->input->post('r1t2'), 'r1t3' => $this->input->post('r1t3'), 'r2t1' => $this->input->post('r2t1'), 'r2t2' => $this->input->post('r2t2'), 'r2t3' => $this->input->post('r2t3'), 'r3t1' => $this->input->post('r3t1'), 'r3t2' => $this->input->post('r3t2'), 'r3t3' => $this->input->post('r3t3'), 'r4t1' => $this->input->post('r4t1'), 'r4t2' => $this->input->post('r4t2'), 'r4t3' => $this->input->post('r4t3'));
            $this->mycache->set_array($key, $arr);
            echo "<script>alert('保存成功');</script>";
            exit();
        }
        $arr = $this->mycache->get_array($key);
        $data ['arr'] = $arr;
        $data ['title'] = "发标预告";

        $data ['tab'] = $this->tab;
        $data ['sub'] = 'trailer';

        $this->load->view('/ychadmin/borrow_tomorrow', $data);
    }

    function borrow_check() {
        $this->load->model('Borrow_model');
        $id = $this->input->post('id');
        $data ['is_check'] = 1;
        $falg = $this->Borrow_model->update($id, $data);

        $this->load->model('log_model');
        $admin = $this->session->userdata('admin_user');
        $username = $admin ['username'];
        $this->log_model->add_log('[发标待审数据检查][借款标][ID==' . $id . '] [' . $username . '已检查]');

        echo $falg;
    }

    public function toloantransferaudit($LoanNoList, $borrow_id, $borrow_fstatus, $index, $indexall, $reviewstr) {
        $this->load->library('/mmm/Rsa', 'rsa');
        $this->load->library('/mmm/Common', 'common');

        $PlatformMoneymoremore = MONEYMOREMOREPID;
        if ($borrow_fstatus == 3) {
            $AuditType = "1";
        } else {
            $AuditType = "2";
        }

        if ($this->common->getAntiState() == 0) {
            $RandomTimeStamp = "";
        } else if ($this->common->getAntiState() == 1) {
            $RandomTimeStamp = $this->common->getRandomTimeStamp();
        }
        $Remark1 = $reviewstr;
        $Remark2 = $borrow_fstatus;
        $Remark3 = $index . '=' . $indexall;
        $ReturnURL = "http://" . $_SERVER ["HTTP_HOST"] . "/moneymoremore/transferauditreturn_verify.html";
        $NotifyURL = "http://" . $_SERVER ["HTTP_HOST"] . "/moneymoremore/transferauditnotify_verify.html";
        $dataStr = $LoanNoList . $PlatformMoneymoremore . $AuditType . $RandomTimeStamp . $Remark1 . $Remark2 . $Remark3 . $ReturnURL . $NotifyURL;
        if ($this->common->getAntiState() == 1) {
            $dataStr = strtoupper(md5($dataStr));
        }
        $SignInfo = $this->rsa->sign($dataStr);

        $officialAction = MONEYMOREMOREURL . "/loan/toloantransferaudit.action";

        echo "<form id=\"formu\" name=\"formu\" action=\"" . $officialAction . "\" method=\"post\">";
        echo "<input id=\"LoanNoList\" name=\"LoanNoList\" value=\"" . $LoanNoList . "\" type=\"hidden\" />";
        echo "<input id=\"PlatformMoneymoremore\" name=\"PlatformMoneymoremore\" value=\"" . $PlatformMoneymoremore . "\" type=\"hidden\" />";
        echo "<input id=\"AuditType\" name=\"AuditType\" value=\"" . $AuditType . "\" type=\"hidden\" />";
        echo "<input id=\"RandomTimeStamp\" name=\"RandomTimeStamp\" value=\"" . $RandomTimeStamp . "\" type=\"hidden\" />";
        echo "<input id=\"Remark1\" name=\"Remark1\" value=\"" . $Remark1 . "\" type=\"hidden\" />";
        echo "<input id=\"Remark2\" name=\"Remark2\" value=\"" . $Remark2 . "\" type=\"hidden\" />";
        echo "<input id=\"Remark3\" name=\"Remark3\" value=\"" . $Remark3 . "\" type=\"hidden\" />";
        echo "<input id=\"ReturnURL\" name=\"ReturnURL\" value=\"" . $ReturnURL . "\" type=\"hidden\" />";
        echo "<input id=\"NotifyURL\" name=\"NotifyURL\" value=\"" . $NotifyURL . "\" type=\"hidden\" />";
        echo "<input id=\"SignInfo\" name=\"SignInfo\" value=\"" . $SignInfo . "\" type=\"hidden\" />";
        echo "<script language=\"javascript\">window.onload=function(){document.formu.submit();}</script>";
        echo "</form>";
    }

}
